import { sys } from "cc";

export class LocalDataManager extends et.Entity{
    private static instance: LocalDataManager;
    public static get Instance(): LocalDataManager {
        return this.instance;
    }
    public static set Instance(value: LocalDataManager) {
        this.instance = value;
    }
    private tempVar = new Map<any, any>();

    public save(key: string, data: any) {
        sys.localStorage.setItem(key, JSON.stringify(data));
    }
    public load<T>(key: string) {
        let jsonStr = sys.localStorage.getItem(key);
        if (jsonStr == null || jsonStr.length == 0) {
            console.error("不存在Key:" + key);
            return null;
        }
        return JSON.parse(jsonStr) as T;
    }
    public remove(key: string) {
        sys.localStorage.removeItem(key);
    }
    public clearData() {
        sys.localStorage.clear();
    }
    public setVar(key, value) {
        this.tempVar.set(key, value);
    }
    public getVar<T>(key) {
        return this.tempVar.get(key) as T;
    }
}
@et.objectSystem(LocalDataManager)
class LocalDataManagerAwakeSystem extends et.AwakeSystem<LocalDataManager>{
    awake(self: LocalDataManager): void {
        LocalDataManager.Instance = self;
    }
}